<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('充值金额列表')"/>
    <th:block th:include="include :: bootstrap-editable-css"/>
    <th:block th:include="include :: jasny-bootstrap-css"/>
    <style>
        .wrap {
            position: relative;
        }

        .sm-mark {
            position: absolute;
            top: 0;
            left: 0;
            margin: 0;
        }

        .xs-mark {
            position: absolute;
            top: 0;
            left: 0;
            margin: 0;
        }

        .sm-mark:before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            border-style: solid;
            border-width: 16px;
            z-index: 1;
            border-color: rgba(206, 3, 5, .7) transparent transparent rgba(206, 3, 5, .7);
        }

        .xs-mark:before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            border-style: solid;
            border-width: 8px;
            z-index: 1;
            border-color: rgba(206, 3, 5, .7) transparent transparent rgba(206, 3, 5, .7);
        }

        .img-sm-text {
            color: #FFF;
            display: inline-block;
            position: absolute;
            top: 0;
            left: 0;
            z-index: 1;
            font-size: 12px;
            text-transform: uppercase;
            width: 20px;
            text-align: center;
        }

        .img-xs-text {
            color: #FFF;
            display: inline-block;
            position: absolute;
            top: 0;
            left: 0;
            z-index: 1;
            font-size: 12px;
            text-transform: uppercase;
            width: 0;
            text-align: center;
        }
    </style>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="product-form">
                <div class="select-list">
                    <ul>
                        <li>
                            <p style="width: 80px">充值编号： </p>
                            <input type="text" name="productSn" value=""/>
                        </li>
                        <li>
                            <p style="width: 80px">名称： </p>
                            <input type="text" name="productName" value=""/>
                        </li>
                        <li>
                            <p style="width: 80px">状态： </p>
                            <select name="isEnable" th:with="type=${@dict.getType('product_status')}">
                                <option value="">全部</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li class="select-time">
                            <p style="width: 80px">创建时间： </p>
                            <input type="text" class="time-input" placeholder="开始时间"
                                   name="params[beginTime]"/>
                            <span>-</span>
                            <input type="text" class="time-input" placeholder="结束时间"
                                   name="params[endTime]"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="formReset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.addTab()" shiro:hasPermission="back:product:add">
                <i class="fa fa-plus"></i> 新增
            </a>
            <a class="btn btn-primary multiple disabled" onclick="multiEdit(1,1)">
                <i class="fa fa-check"></i> 批量上架
            </a>
            <a class="btn btn-danger multiple disabled" onclick="multiEdit(1,0)">
                <i class="fa fa-ban"></i> 批量下架
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-table-editable-js"/>
<th:block th:include="include :: jasny-bootstrap-js"/>
<script th:inline="javascript">
    const editFlag = [[${@permission.hasPermi('back:product:edit')}]];
    const removeFlag = [[${@permission.hasPermi('back:product:remove')}]];
    const productStatusDict = [[${@dict.getType('product_status')}]];
    const isEnableDict = [[${@dict.getType('is_enable')}]];
    const prefix = ctx + "back/product";

    $(function () {
        var options = {
            url: prefix + "/searchMemberRechargeList",
            createUrl: prefix + "/add",
            removeUrl: prefix + "/remove",
            sortName: "orderNum asc,createTime",
            sortOrder: "desc",
            modalName: "充值金额",
            paginationShowPageGo: true,
            clickToSelect: true,
            onEditableSave: onEditableSave,
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'productId',
                    title: '充值金额ID',
                },
                {
                    field: 'productSn',
                    title: '条码',
                    sortable: true
                },
                {
                    field: 'productName',
                    title: '名称',
                    sortable: true
                },
                {
                    field: 'price',
                    title: '参考金额',
                    sortable: true
                },
                {
                    field: 'orderNum',
                    title: '排序',
                    editable: {
                        type: 'text',
                        title: '排序',
                        emptytext: "【排序】不能为空",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【排序】不能为空!';
                            }
                            if (isNaN(v)) {
                                return '【排序】必须是数字';
                            }
                            if (!(/(^[0-9]\d*$)/.test(v))) {
                                return '请输入正确的【排序】';
                            }
                        }
                    }
                },
                {
                    field: 'isEnable',
                    title: '状态',
                    editable: {
                        type: 'select',
                        title: '状态',
                        source: [{
                            value: 1,
                            text: "在售"
                        }, {
                            value: 0,
                            text: "停售"
                        }]
                    }
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    sortable: true
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-trash"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function onEditableSave(field, row) {
        let updateData = {};
        updateData[field] = row[field];
        updateData.id = row.id;
        $.ajax({
            type: 'post',
            url: ctx + "back/product/edit",
            data: updateData,
            dataType: 'json',
            async: false,
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.alertSuccess("修改成功");
                } else {
                    $.modal.alertError(result.msg);
                }
            }
        });
    }

    function multiEdit(type, val) {
        var rows = $.table.selectFirstColumns();
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        let updateData = {
            ids: rows.join()
        };
        switch (parseInt(type)) {
            case 1:
                //批量上架/下架
                updateData.isEnable = val;
                break;
            default:
                $.modal.alertWarning("操作失败！");
                return;
        }
        $.ajax({
            type: 'post',
            url: ctx + "back/product/edit",
            data: updateData,
            dataType: 'json',
            async: false,
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.alertSuccess("操作成功！");
                } else {
                    $.modal.alertError(result.msg);
                }
                $.table.refresh();
            }
        });
    }

    function formReset() {
        $('input[name="categoryIdStr"]').val("");
        $.form.reset();
    }
</script>
</body>
</html>